import {
  Controller,
  Get,
  Post,
  Body,
  Headers,
  Query,
  Param,
  Req,
  Res,
  Put,
  Delete,
  Patch,
} from '@nestjs/common';
import { v4 as uuidv4 } from 'uuid';
import { ProxyService } from './proxy.service';

@Controller()
export class ProxyController {
  constructor(private readonly appService: ProxyService) {}

  @Get('/forward')
  async forward(
    @Headers() headers: Record<string, string>,
    @Query() query: Record<string, any>,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    return this.handleForward({
      method: 'GET',
      body: {},
      headers,
      query,
      req,
      res,
    });
  }

  @Post('/forward')
  async postForward(
    @Body() body: any,
    @Headers() headers: Record<string, string>,
    @Query() query: Record<string, any>,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    return this.handleForward({
      method: 'POST',
      body,
      headers,
      query,
      req,
      res,
    });
  }

  @Put('/forward')
  async putForward(
    @Body() body: any,
    @Headers() headers: Record<string, string>,
    @Query() query: Record<string, any>,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    return this.handleForward({
      method: 'PUT',
      body,
      headers,
      query,
      req,
      res,
    });
  }

  @Delete('/forward')
  async deleteForward(
    @Body() body: any,
    @Headers() headers: Record<string, string>,
    @Query() query: Record<string, any>,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    return this.handleForward({
      method: 'DELETE',
      body,
      headers,
      query,
      req,
      res,
    });
  }

  @Patch('/forward')
  async patchForward(
    @Body() body: any,
    @Headers() headers: Record<string, string>,
    @Query() query: Record<string, any>,
    @Req() req: Request,
    @Res() res: Response,
  ) {
    return this.handleForward({
      method: 'PATCH',
      body,
      headers,
      query,
      req,
      res,
    });
  }

  async handleForward(params) {
    const { method, body, headers, query, req, res } = params;
    const uuid = query['uuid'];
    const requestId = uuidv4();
    const url = decodeURIComponent(query['forwardurl']);
    delete query['forwardurl'];
    if (!url) {
      throw new Error('x-forward-url header is required');
    }

    const {
      'x-forward-url': forwardUrl,
      'content-length': contentLength,
      ...forwardHeaders
    } = headers;

    console.log(forwardUrl, contentLength);

    this.appService.forwardHttp({
      url,
      method,
      data: body,
      headers: forwardHeaders,
      params: query,
      uuid,
      requestId,
    });

    this.appService.sendDataByCycle(uuid, requestId, res);
  }

  @Post('solve-request/:uuid/:requestId')
  async solveRequest(
    @Param('uuid') uuid: string,
    @Param('requestId') requestId: string,
    @Body() body: any,
  ) {
    body.uuid = uuid;
    body.requestId = requestId;
    return await this.appService.solveRequest(body);
  }

  @Get('getAllRequests')
  getAllRequests(@Query('uuid') uuid: string) {
    return this.appService.getAllRequests(uuid);
  }
}
